package com.hyt.it.ogt.controller.pj;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.pj.common.Constants;
import com.hyt.it.ogt.pj.common.ResponseCode;
import com.hyt.it.ogt.pj.model.entity.Certificate;
import com.hyt.it.ogt.pj.service.ICertificateService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;

/**
 * <p>
 * 证书表 前端控制器
 * </p>
 *
 * @author qiaozj
 * @since 2020-07-09
 */
@ApiSort(value = 23)
@Api(tags = "23.证书接口", value = "证书接口")
@RestController
@RequestMapping("/certificate")
public class PjCertificateController extends BaseController{

    @Autowired
    private ICertificateService certificateService;

    @ApiOperationSort(value = 1)
    @ApiOperation(value = "23.1.1根据id获取证书", notes = Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("query/{id}")
    public Object query(@PathVariable("id") String id){
        Certificate certificate = certificateService.getById(id);
        if(certificate != null){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(certificate).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 2)
    @ApiOperation(value = "23.1.2获取项目所有证书", notes= Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("findProjectCertificate")
    public Object findProjectCertificates(@RequestParam String projectId){
        List<Certificate> certificates = certificateService.findProjectCertificates(projectId);
        if(certificates != null){
            return ApiResponse.builder().code(235201).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(certificates).build();
        }
    }

    @ApiOperationSort(value = 10)
    @ApiOperation(value = "23.2.1添加证书", notes= Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("add")
    public Object add(@RequestBody Certificate certificate){
        Boolean flag = certificateService.save(certificate);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 20)
    @ApiOperation(value = "23.3.1更新证书", notes = Constants.SWAGGER_METHOD_NOTES)
    @PutMapping("update")
    public Object update(@RequestBody Certificate certificate){
        boolean flag = certificateService.updateById(certificate);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 30)
    @ApiOperation(value = "23.4.1根据id删除证书", notes = Constants.SWAGGER_METHOD_NOTES)
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable("id") String id){
        boolean flag = certificateService.removeById(id);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 31)
    @ApiOperation(value = "23.4.2根据项目id删除证书", notes = Constants.SWAGGER_METHOD_NOTES)
    @DeleteMapping("deleteByProjectId")
    public Object deleteByProjectId(@RequestParam("projectId") String projectId){
        boolean flag = certificateService.deleteByProjectId(projectId);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

}
